package org.jeecg.modules.renlmd.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.kuawcsFs.entity.AgKuawcsFs;
import org.jeecg.modules.kuawcsFs.service.IAgKuawcsFsService;

import org.jeecg.modules.renlmd.entity.HqStRenlmdMx;
import org.jeecg.modules.renlmd.service.IHqStRenlmdMxService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.time.*;
import java.util.*;

@Slf4j
public class syncRenlmdMx implements Job {
    @Autowired
    private IHqStRenlmdMxService hqStRenlmdMxService;

    @Autowired
    private IAgKuawcsFsService agKuawcsFsService;

    @Autowired
    RestTemplate restTemplate;

    @Value("${kwakcs.savePath}")
    String savePath ;


    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        System.out.println((" 同步人流密度明细记录，任务执行了，时间:" + DateUtils.getTimestamp()));
        syncRenlmdMx();
    }

     void syncRenlmdMx() {
//读取数据
         List <HqStRenlmdMx> hqStRenlmdMx = hqStRenlmdMxService.list(new QueryWrapper <HqStRenlmdMx>().eq("chuanswczt","0"));

         if (!hqStRenlmdMx.isEmpty()){

             JSONArray jsonArray = new JSONArray();

             for (int i = 0; i < hqStRenlmdMx.size(); i++) {
                 jsonArray.add(hqStRenlmdMx.get(i));
             }

             //写为文件
             BufferedWriter writer = null;
             //		String savePath="D:/Json测试";
             String fileName="hq_st_renlmd_mx"+".json";
             String path =savePath+"/数据同步/"+fileName;
             File file = new File(path);
             String parentPath = file.getParent();
             File parentFile = new File(parentPath);
             if(!parentFile.exists()) {
                 parentFile.mkdirs();
             }
             //如果文件不存在，则新建一个
             if(!file.exists()){
                 try {
                     file.createNewFile();

                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             // 格式化json字符串
             String jsonData= JSON.toJSONString(jsonArray);
             //写入
             try {
                 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
                 writer.write(jsonData);
             } catch (IOException e) {
                 e.printStackTrace();
             }finally {
                 try {
                     if(writer != null){
                         writer.close();
                         System.out.println("文件生成成功！");

                     }
                 } catch (IOException e) {
                     e.printStackTrace();
                     System.out.println("文件生成失败！");
                 }
             }

             hqStRenlmdMx.forEach(item -> {
                 item.setChuanswczt("1");
                 hqStRenlmdMxService.updateById(item);

             });


             //调用跨网传输
             AgKuawcsFs agKuawcsFs=new AgKuawcsFs();
             agKuawcsFs.setFasmbbm("houqin_erweima");
             agKuawcsFs.setFaswjm(fileName);
             agKuawcsFs.setFaswjlj(path);
             agKuawcsFs.setChuansfs("2");
             agKuawcsFs.setFassj(new Date());
             //  agKuawcsFsService.save(agKuawcsFs);
             agKuawcsFsService.sendFile(agKuawcsFs);
         }


    }


}
